package service

import (
	"time"
	"vrcm/src/vrcm/app/admin/models/monitor"
	dto "vrcm/src/vrcm/app/admin/service/dto/monitor"
	cDto "vrcm/src/vrcm/common/dto"
	"vrcm/src/vrcm/common/service"
)

type AuditVideo struct {
	service.Service
}

func (e *AuditVideo) GetPage(c *dto.AuditVideoGetPageReq, list *[]monitor.AuditVideo, count *int64) error {
	var err error
	var data monitor.AuditVideo

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

func (e *AuditVideo) Get(d *dto.AuditVideoGetReq) ([]monitor.AuditVideo, error) {
	models := make([]monitor.AuditVideo, 0, 5)
	db := e.Orm.Model(&monitor.AuditVideo{})
	if d.Id > 0 {
		db.Where("id=?", d.Id)
	}
	if d.EventId > 0 {
		db.Where("event_id=?", d.EventId)
	}

	if d.MemberId > 0 {
		db.Where("member_id=?", d.MemberId)
	}
	err := db.Find(&models).Error
	if err != nil {
		e.Log.Errorf("get error:%s", err)
		return models, err
	}
	return models, nil
}

func (e *AuditVideo) Insert(c *dto.AuditVideoInsertReq) error {
	now := time.Now().Unix()
	var err error
	var data monitor.AuditVideo
	data.TenantId = c.TenantId
	data.ProgramId = c.ProgramId
	data.EventId = c.EventId
	data.MemberId = c.MemberId
	data.AuditTime = c.AuditTime
	data.AuditImg = c.AuditImg
	data.SignImg = c.SignImg
	data.AuditTag = c.AuditTag
	data.MemberName = c.MemberName
	data.IsShare = 2
	data.CreateTime = now
	data.UpdateTime = now
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("创建审片记录:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

func (e *AuditVideo) Update(c *dto.AuditVideoUpdateReq) error {

	var err error
	var data monitor.AuditVideo
	err = e.Orm.Model(&monitor.AuditVideo{}).Where("id=?", c.Id).Find(&data).Error
	if err != nil {
		e.Log.Errorf("get AuditVideo:%s \r\n", err)
		return err
	}

	data.MemberName = c.MemberName
	data.AuditTime = c.AuditTime
	data.AuditImg = c.AuditImg
	data.SignImg = c.SignImg
	data.AuditTag = c.AuditTag
	data.UpdateTime = time.Now().Unix()
	err = e.Orm.Save(&data).Error
	if err != nil {
		e.Log.Errorf("更新审片失败:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

func (e *AuditVideo) Remove(d *dto.AuditVideoDeleteReq) error {
	err := e.Orm.Model(&monitor.AuditVideo{}).Where("id = ?", d.Id).Delete(&monitor.AuditVideo{}).Error
	if err != nil {
		e.Log.Errorf("删除审片失败:%s \r\n", err)
		return err
	}
	return nil
}

func (e *AuditVideo) ShareAuditVideo(d *dto.ShareAuditVideoReq) error {
	err := e.Orm.Model(&monitor.AuditVideo{}).Where("id = ?", d.Id).Update("is_share", 1).Error
	if err != nil {
		e.Log.Errorf("删除审片失败:%s \r\n", err)
		return err
	}
	return nil
}
